<template>
  <div>
    <Spin :spinning="loading" tip="Loading...">
      <div style="padding-top: 1rem;"></div>
    <!-- table -->
      <FormPanel ref="formPanel" :list="formList1" :cols="1" :formType="'add'" labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
      <BaseDialog :visible.sync="visibleDrawer" :title="'自费车辆配件类型保修期新增'" destroyOnClose :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
        <vin @close="closeAdd" @save="saveAdd"></vin>
      </BaseDialog>
      <div style="padding-top: 1rem;"></div>
      <div
        :style="{
          position: 'absolute',
          left: 0,
          right: 0,
          zIndex: 9,
          borderTop: '1px solid #e9e9e9',
          padding: '10px 20px',
          background: '#fff',
          textAlign: 'right'
        }"
      >
        <multiuse-button type="primary" :click="saveDialog.bind()">保 存</multiuse-button>
        <multiuse-button @click="closeDialog">关 闭</multiuse-button>
      </div>
    </Spin>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import {  getPartType } from '@/api/dictionary/index';
import { getListByPage, newSave, piliangqiyong, piliangtingyong, excelExport, editSave } from '@/api/claim/factory/SelfPartClaimWarrantyMaintain';
import { getPart } from '@/api/dictionary/index';
import { getAppId} from '@/assets/js/auth';
export default {
  mixins: [dictionary],
  data() {
    return {
      loading: false,
      dataList1: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      visibleAdd: false,
      infoId: null,
      saveData: null,
      flterValue: {}, //参数
      res: {}, //返回值
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList1: this.createFormList1(),
      activeName: '审核记录'
    };
  },
  created() {
    this.chuqi();
    if(getAppId()=='beijing'){
      this.formList1.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
    }else{
      this.formList1.find((x) => x.fieldName == 'powerType').itemList = this.createDictList('6008');
      this.formList1.find((x) => x.fieldName == 'powerType').initialValue = 60081002;
      this.formList1.find((x) => x.fieldName == 'powerType').disabled=true;
    }
  },
  methods: {
    async chuqi(){
      const res = await getPart();
      if (res.resultCode == 200) {
        this.formList1.find(x => x.fieldName == 'typeId').initialValue = '';
        this.formList1.find(x => x.fieldName == 'typeId').itemList = res.data.map(y => ({ text: y.name, value: y.code }));
      }
    },
    formChangeHandle(val) {
      console.log(val);
      this.saveData = val;
    },
    //保存
    chooseOrderNo() {
      this.visibleDrawer = true;
    },
    closeAdd() {
      this.visibleDrawer = false;
      this.fetchParams = { ...this.fetchParams };
    },
    saveAdd() {
      this.visibleDrawer = false;
      this.fetchParams = { ...this.fetchParams };
    },
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData !== null) {
        this.loading = true;
        let res = await newSave(this.saveData);
        console.log(res);
        if (res.resultCode === 200) {
          this.$emit('save');
          return this.$notify.success({title: '提示信息', message: '保存成功'});
        } else {
          this.loading = false;
          // this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          // this.closeDialog();
        }
      }
    },
    //取消操作
    closeDialog(type) {
      if(type == 'refresh'){
        this.$emit('save');
      } else {
        this.$emit('close', { visibleAdd: false, data: this.selectedRows, type: 'close' });
      }

    },
    add() {},
    createFormList1() {
      return [
        // {
        //   type: 'SELECT',
        //   label: '配件类型',
        //   fieldName: 'typeId',
        //   placeholder: '配件类型',
        //   style: { width: '200px' },
        //   itemList: this.createDictList('5581')
        // },
        {
          type: 'SELECT',
          label: '配件类型',
          filterable: true,
          fieldName: 'typeId',
          placeholder: '请选择配件类型...',
          initialValue: '',
          rules: [{ required: true, message: '请选择配件类型...', trigger: 'change' }],
          itemList: []
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择动力形式...',
          initialValue: '',
          itemList: this.createDictList('6008'),filterable:true,
          rules: [{required: true, message: '请选择动力形式...', trigger: 'change'}]
          },
        {
          type: 'INPUT',
          label: '保修期',
          fieldName: 'spPeriod',
          placeholder: '请输入保修期...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修期...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                callback();
              },
              trigger: ['blur', 'change'],
            },
          ],
          unitRender: () => {
            return <span>(月)</span>;
          },
        },
        {
          type: 'INPUT',
          label: '保修里程',
          fieldName: 'spMile',
          placeholder: '请输入保修里程...',
          rules: [
            {
              required: true,
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入保修里程...'));
                }
                if (!Number.isInteger(Number(value))) {
                  return callback(new Error('请输入数字值...'));
                }
                callback();
              },
              trigger: ['blur', 'change'],
            },
          ],
          unitRender: () => {
            return <span>(KM)</span>;
          },
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'spStatus',
          filterable: true,
          placeholder: '请输入状态...',
          itemList: [{text: '禁用', value: 0}, {text: '启用', value: 1}],
          rules: [{required: true, message: '请选择状态...', trigger: 'change'}]
        }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
